﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using QuocTeViet.DO;
using QuocTeViet.BL;

namespace QuocTeViet
{
    public partial class Category : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            load();
        }

        public void load()
        {
            if (Request.QueryString["ID"] != null)
            {
                int id = int.Parse(Request.QueryString["ID"].ToString());
                //int kt = new cmsCategoryBL().ktraParent(id);

                //if (kt < 1)
                //{
                //    bindatalist(new cmsCategoryBL().SelectMultiArtCat(id));
                //}
                //else if (id == 7 && kt > 1)
                //{
                //    // Chỗ này chưa ổn, nó ra 1 đống CatID, em định merge table mà ko được...:-s
                //    // 
                bindatalist(new DataView(new cmsCategoryBL().SelectAllArticleOfChildrenCat(id),"","ArticleID DESC",DataViewRowState.CurrentRows).ToTable());
                //}
                //else if (id != 7 && kt > 1)
                //    bindatalist2(new cmsCategoryBL().SelectByParentID(id, 0));

                if (Session["lang"].ToString() == "VN")
                    Page.Title = new cmsCategoryBL().Select(new cmsCategoryDO { CategoryID = int.Parse(Request.QueryString["ID"].ToString()) }).Title + " - " + new sysConfigBL().Select(new sysConfigDO { ConfigID = 12 }).ConfigValue ;
                else
                    Page.Title = new cmsCategoryBL().Select(new cmsCategoryDO { CategoryID = int.Parse(Request.QueryString["ID"].ToString()) }).eTitle + " - " + new sysConfigBL().Select(new sysConfigDO { ConfigID = 12 }).ConfigValue ;
            }
        }
        public string Change_AV(string ip_str_change)
        {
            return Ultility.Change_AV(ip_str_change);
        }
        public int CurrentPage
        {
            get
            {
                // look for current page in ViewState
                object o = this.ViewState["_CurrentPage"];
                if (o == null)
                    return 0;	// default to showing the first page
                else
                    return (int)o;
            }

            set
            {
                this.ViewState["_CurrentPage"] = value;
            }
        }

    

        private void bindatalist(DataTable dt)
        {
            rpt2.Visible = true;
            rpt.Dispose();
            PagedDataSource objPds = new PagedDataSource();
            objPds.DataSource = dt.DefaultView;
            objPds.AllowPaging = true;
            objPds.PageSize = 10;
            objPds.CurrentPageIndex = CurrentPage;
            cmdPrev.Visible = !objPds.IsFirstPage;
            cmdNext.Visible = !objPds.IsLastPage;
            if (dt.Rows.Count > 0)
            {
                rpt.Visible = true;
                rpt.DataSource = objPds;
                rpt.DataBind();
            }
        }
        private void bindatalist2(DataTable dt)
        {
            rpt.Visible = false;
            rpt2.Dispose();
            PagedDataSource objPds = new PagedDataSource();
            objPds.DataSource = dt.DefaultView;
            objPds.AllowPaging = true;
            objPds.PageSize = 10;
            objPds.CurrentPageIndex = CurrentPage;
            cmdPrev.Visible = !objPds.IsFirstPage;
            cmdNext.Visible = !objPds.IsLastPage;
            if (dt.Rows.Count > 0)
            {
                rpt2.Visible = true;
                rpt2.DataSource = objPds;
                rpt2.DataBind();
            }
        }
        protected void cmdPrev_Click(object sender, ImageClickEventArgs e)
        {
            CurrentPage -= 1;

            // Reload control
            load();
        }
        protected void cmdNext_Click(object sender, ImageClickEventArgs e)
        {
            CurrentPage += 1;
            load();
        }
    }
}
